package com.fairytask.ui
{
	import com.greensock.TimelineLite;
	import com.greensock.TweenNano;
	import com.fairytask.engine.GameStage;
	import com.fairytask.engine.components.text.StaticText;
	import com.fairytask.engine.define.Filter;
	import com.fairytask.engine.resource.StaticIni;
	import com.jzonl.event.ChatEvent;
	import com.fairytask.ui.base.FrameUI;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * 广播提示 
	 * @author nanz
	 * 
	 */	
	public class BroadcastUI extends FrameUI
	{
		private static var _self		:BroadcastUI;
		
		public var bugleFirst			:MovieClip;//喇叭(游戏公告)
		public var bugle				:MovieClip;//喇叭(玩家喇叭)
		
		public var bgSystem			:MovieClip;//背景(系统)
		public var bgBarFirt			:MovieClip;//灰色背景条(游戏剧情)
		public var bgBarSecond			:MovieClip;//灰色背景条(玩家喇叭)
		
		public var systemInfoLayer		:Sprite =	new Sprite();//系统
		public var firstInfoLayer		:Sprite =	new Sprite();//游戏剧情
		public var secondInfoLayer		:Sprite	=	new Sprite();//玩家喇叭
		
		private var _maskSystem		:Sprite = 	new Sprite();//遮罩(系统)
		private var _mskLayerSystem	:Sprite	= 	new Sprite();//被遮罩显示层(系统)
		
		private var _maskFirst			:Sprite	=	new Sprite();//遮罩(游戏剧情)
		private var _mskLayerFirst		:Sprite	=	new Sprite();//被遮罩显示层(游戏剧情)
		
		private var _mask				:Sprite	=	new Sprite();//遮罩(玩家喇叭)
		private var _mskLayer			:Sprite	=	new Sprite();//被遮罩显示层(玩家喇叭)
		
		private var timelineFirst		:TimelineLite	= 	new TimelineLite();
		private var timeline			:TimelineLite	=	new TimelineLite();
		
		private var _bugleArr			:Array	= [];//玩家喇叭数据缓存
		private var _systemArr			:Array 	= [];//系统
		private var _broadCastArr		:Array	= [];//游戏剧情提示
		
		private var _bugleLab			:StaticText;
		private var _systemLab			:StaticText;
		private var _broadCastLab		:StaticText;
		public static function getInstance():BroadcastUI
		{
			if(null	==	_self)
			{
				_self	=	new BroadcastUI();				
			}
			return _self;
		}
		
		public function BroadcastUI(popUI:Boolean=true)
		{
			super(popUI);
			init();
			initEvent();
		}
		
		private function draw(bin:Sprite,width:int,height:int):void
		{
			bin.graphics.beginFill(0);
			bin.graphics.drawRect(0,0,width,height);
			bin.graphics.endFill();
		}
		
		private function init():void
		{
			_bugleLab = new StaticText("FFFFFF");
			_systemLab= new StaticText("FF0000");
			_broadCastLab = new StaticText();
			_bugleLab.labelFilter = _systemLab.labelFilter = Filter.COMMON_TEXT_FILTER;
			
			initY		= 	this.y;
			
			draw(_maskSystem,490,20);//绘制遮罩层(系统)
			draw(_maskFirst,500,25);//绘制遮罩层(金钱道具)
			draw(_mask,500,25);		//绘制遮罩层(活动或公告)

			_maskSystem.x = 5;
			_mskLayerSystem.x = 5;
			_mskLayerSystem.mask = _maskSystem;
			
			_mskLayerFirst.x = 26;
			_maskFirst.x 	 = 26;
			_mskLayerFirst.mask = _maskFirst;
			
			_mskLayer.x	=	26;
			_mask.x		=	26;
			_mskLayer.mask	=	_mask;
			
			systemInfoLayer.addChild(_mskLayerSystem);
			systemInfoLayer.addChild(_maskSystem);
			
			firstInfoLayer.addChild(_mskLayerFirst);
			firstInfoLayer.addChild(_maskFirst);
			
			secondInfoLayer.addChild(_mskLayer);//被遮罩层		位于下层
			secondInfoLayer.addChild(_mask);	//遮罩层			位于上层
			
			addChild(systemInfoLayer);
			addChild(firstInfoLayer);
			addChild(secondInfoLayer);
			systemInfoLayer.x 	= 30;
			systemInfoLayer.y 	=  -23;
			firstInfoLayer.y 	= 0;
			secondInfoLayer.y 	= 23;
			systemInfoLayer.visible = false;
			firstInfoLayer.visible = false;
			secondInfoLayer.visible= false;
			windowBG.addChild(systemInfoLayer);
			windowBG.addChild(firstInfoLayer);
			windowBG.addChild(secondInfoLayer);
			
			this.mouseEnabled 	= false;
			this.mouseChildren 	= false;
		}
		private function initEvent():void
		{
//			model.addEventListener(ChatEvent.BROADCAST_MESSAGE,onBroadCast);
//			model.addEventListener(ChatEvent.BROADCAST_SYSTEM_MESSAGE,onSystem);
		}
		
		/**
		 * 玩家喇叭（对外接口）
		 * @param str
		 * 
		 */		
		public function onFirstBroadCast(str:String):void
		{
			_bugleArr.push(StaticIni.getSwfLangStrVar("4002001414",[str]));
			if(!isShow)
			{
				show();
			}
			if(!secondInfoLayer.visible)
			{
				this.parent.mouseEnabled = false;
				secondInfoLayer.visible = 	true;
				_bugleLab.htmlByBorder =	_bugleArr.shift();
				_bugleLab.y	=	-5;
				_bugleLab.x	=	526;
				_mskLayer.addChild(_bugleLab);
				var tm	:Number	=	20*(_bugleLab.width+_mask.width)/500;
				TweenNano.to(_bugleLab,tm,{x:-_bugleLab.width,onComplete:onAlpha,onCompleteParams:[_bugleLab,1]});
			}
		}
		
		/**
		 * 系统 
		 * @param evt
		 * 
		 */		
		public function onSystem(evt:ChatEvent):void
		{
			_systemArr.push(evt.clientMsg);
			if(!isShow)
			{
				show();
			}
			
			if(!systemInfoLayer.visible)
			{
				this.parent.mouseEnabled = false;
				systemInfoLayer.visible = true;
				_systemLab.htmlByBorder	=	_systemArr.shift();
				_systemLab.x	=	526;
				_mskLayerSystem.addChild(_systemLab);
				var tm	:Number	=	20*(_systemLab.width+_maskSystem.width)/500;
				TweenNano.to(_systemLab,tm,{x:-_systemLab.width,onComplete:onAlpha,onCompleteParams:[_systemLab,3]});
			}
		}
		
		override public function show(space:String=TOPLAYER, onshow:Function=null):void
		{
			super.show(space);
		}
	
		/**
		 * 游戏剧情
		 * @param evt
		 * 
		 */				
		public function onBroadCast(evt:ChatEvent):void
		{
			_broadCastArr.push(StaticIni.getSwfLangStrVar("4002001413",[evt.clientMsg]));
			if(!isShow)
			{
				show();
			}
		
			if(!firstInfoLayer.visible)
			{
				this.parent.mouseEnabled = false;
				firstInfoLayer.visible = true;
				_broadCastLab.htmlByBorder	=	_broadCastArr.shift();
				_broadCastLab.y	=	-5;
				_broadCastLab.x	=	526;
				_mskLayerFirst.addChild(_broadCastLab);
				var tm	:Number	=	20*(_broadCastLab.width+_maskFirst.width)/500;
				TweenNano.to(_broadCastLab,tm,{x:-_broadCastLab.width,onComplete:onAlpha,onCompleteParams:[_broadCastLab,2]});
			}
		}
		
		private function onAlpha(target:DisplayObject,sort:int):void
		{
			switch(sort)
			{
				case 1:TweenNano.to(target,0.5,{onComplete:onFirstComplete,onCompleteParams:[target]});break;
				case 2:TweenNano.to(target,0.5,{onComplete:complete,onCompleteParams:[target]});break;
				case 3:TweenNano.to(target,0.5,{onComplete:systemComplete,onCompleteParams:[target]});break;
			}
		}
		
		/**
		 * 剧情提示播放完
		 * @param target
		 * 
		 */		
		private function complete(target:DisplayObject):void
		{
			if(_broadCastLab.parent)
			{
				_broadCastLab.parent.removeChild(_broadCastLab);
			}	
			if(_broadCastArr.length>0)
			{
				_broadCastLab.htmlByBorder = _broadCastArr.shift();
				_broadCastLab.y	=	-5;
				_broadCastLab.x	=	526;
				_mskLayerFirst.addChild(_broadCastLab);
				var tm	:Number	=	20*(_broadCastLab.width+_maskFirst.width)/500;
				TweenNano.to(_broadCastLab,tm,{x:-_broadCastLab.width,onComplete:onAlpha,onCompleteParams:[_broadCastLab,2]});
			}else
			{
				firstInfoLayer.visible= false;
				if(!secondInfoLayer.visible)
				{
					if(this.parent)
					{
						this.parent.mouseEnabled = true;
					}
				}
			}	
		}
	
		/**
		 * 玩家一条喇叭播放完
		 * @param target
		 * 
		 */		
		private function onFirstComplete(target:DisplayObject):void
		{
			if(_bugleLab.parent)
			{
				_bugleLab.parent.removeChild(_bugleLab);
			}
			if(_bugleArr.length>0)
			{
				_bugleLab.htmlByBorder = _bugleArr.shift();
				_bugleLab.y	=	-5;
				_bugleLab.x	=	526;
				_mskLayer.addChild(_bugleLab);
				var tm	:Number	=	20*(_bugleLab.width+_mask.width)/500;
				TweenNano.to(_bugleLab,tm,{x:-_bugleLab.width,onComplete:onAlpha,onCompleteParams:[_bugleLab,1]});
			}else
			{
				secondInfoLayer.visible = false;
				if(!firstInfoLayer.visible)
				{
					if(this.parent)
					{
						this.parent.mouseEnabled = true;
					}
				}
			}
		}
		
		/**
		 * 系统消息一条播放完 
		 * @param target
		 * 
		 */		
		private function systemComplete(target:DisplayObject):void
		{
			if(_systemLab.parent)
			{
				_systemLab.parent.removeChild(_systemLab);
			}
			if(_systemArr.length>0)
			{
				_systemLab.htmlByBorder = _systemArr.shift();
				_systemLab.x	=	526;
				_mskLayerSystem.addChild(_systemLab);
				var tm	:Number	=	20*(_systemLab.width+_maskSystem.width)/500;
				TweenNano.to(_systemLab,tm,{x:-_systemLab.width,onComplete:onAlpha,onCompleteParams:[_systemLab,3]});
			}else
			{
				systemInfoLayer.visible = false;
			}
		}
		
		/**
		 * 场景变化改变位置 
		 * 
		 */
		override protected function stageReSize(evt:Event=null):void
		{
			this.y	= 	- GameStage.y + initY;
		}
	}
}